package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    ////Excel文件中的数据保存到数据库
    @Override
    public void add(List<OrderSetting> orderSettingList) {

        if(orderSettingList != null && orderSettingList.size() > 0){

            for (OrderSetting orderSetting : orderSettingList) {
                AddOrEdit(orderSetting);
            }
        }
    }

    //根据当前日期查询数据库  2020-3
    @Override
    public List<Map> getOrderSettingByMonth(String date) {//yyyy-MM
        /*
        由于每个月份的天数不确定，所以用最简便的方式去实现
        即每个月份都从1号查询到31号

        SELECT * FROM t_ordersetting WHERE orderDate BETWEEN '2020-6-1' AND '2020-6-31';

        */
        String begin = date + "-1";//2020-6-1
        String end = date + "-31";//2020-6-31
        //封装map，传递给dao层
        Map<String, String> map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        //调用dao层，根据日期范围查询数据
        List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingByMonth(map);

        //组装返回结果集 [{date:1,number:120,reservations:1},{...},{...},{...}]
        //将orderSettingList取出重新装到新的list集合中
        ArrayList<Map> list = new ArrayList<>();

        //前端需要的数据格式{ date: 11, number: 120, reservations: 1 },
        for (OrderSetting orderSetting : orderSettingList) {
            Map<String, Object> map1 = new HashMap<>();

            /*测试数据
            int date1 = orderSetting.getOrderDate().getDate();
            System.out.println(date1);*/

            //往map中添加数据{ date: 11, number: 120, reservations: 1 }
            map1.put("date",orderSetting.getOrderDate().getDate());//当前日期是几号
            map1.put("number",orderSetting.getNumber());//可预约人数
            map1.put("reservations",orderSetting.getReservations());//已预约人数

            list.add(map1);
        }
        return list;
    }

    //根据日期修改预约设置
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //同样需要先判断当前日期有没有数据，要是有就是更新操作，要是没有就是添加操作
        AddOrEdit(orderSetting);
    }

    //抽取代码 先判断当前日期有没有数据，要是有就是更新操作，要是没有就是添加操作
    private void AddOrEdit(OrderSetting orderSetting){
        //根据日期查询当前数据是否存在，存在则是更新操作，不存在则是添加操作
        Long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());

        if(count > 0){
            //存在，更新操作
            orderSettingDao.editNumberByOrderDate(orderSetting);

        }else {
            //不存在，添加操作
            orderSettingDao.add(orderSetting);
        }
    }

}
